package com.shenmazong.servertalkmarket.controller;

import com.shenmazong.servertalkmarket.service.MarketService;
import com.shenmazong.utils.ResponseResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@CrossOrigin
public class MarketController {

    @Autowired
    MarketService marketService;

    @ApiOperation(value = "添加活动消息", notes = "添加活动消息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "userId", value = "管理员ID", dataType = "Integer", required = true, example = "0"),
            @ApiImplicitParam(name = "message", value = "活动消息", dataType = "String", required = true)
    })
    @PostMapping(value = "/addMarketMessage")
    public ResponseResult addMarketMessage(
            @RequestParam("userId") Integer userId,
            @RequestParam("message") String message
    ) {
        return marketService.addMarketMessage(userId, message);
    }

    @ApiOperation(value = "删除活动消息", notes = "删除活动消息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "userId", value = "管理员ID", dataType = "Integer", required = true, example = "0"),
            @ApiImplicitParam(name = "msgId", value = "消息ID", dataType = "Integer", required = true, example = "0")
    })
    @PostMapping(value = "/delMarketMessage")
    public ResponseResult delMarketMessage(
            @RequestParam("userId") Integer userId,
            @RequestParam("msgId") Integer msgId
    ) {
        return marketService.delMarketMessage(userId, msgId);
    }

    @ApiOperation(value = "发布活动消息", notes = "发布活动消息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "userId", value = "管理员ID", dataType = "Integer", required = true, example = "0"),
            @ApiImplicitParam(name = "msgId", value = "消息ID", dataType = "Integer", required = true, example = "0")
    })
    @PostMapping(value = "/publishMarketMessage")
    public ResponseResult publishMarketMessage(
            @RequestParam("userId") Integer userId,
            @RequestParam("msgId") Integer msgId
    ) {
        return marketService.publishMarketMessage(userId, msgId);
    }

    @ApiOperation(value = "获取活动消息", notes = "获取活动消息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "userId", value = "管理员ID", dataType = "Integer", required = true, example = "0")
    })
    @PostMapping(value = "/getMarketMessage")
    public ResponseResult getMarketMessage(
            @RequestParam("userId") Integer userId
    ) {
        return marketService.getMarketMessage();
    }

    //--1 检查今天是否已经举行过活动了
    //--2 特价课信息同步写入到mysql中（定义一个时间）


    //--3 定时任务：从mysql读出特价课的信息到redis（list）

    //--4 点击抢购按钮：（检查是否已经抢过了）到redis获取课程信息，抢到课程的用户id发到redis（抢到集合set）
    //--5 把抢购信息添加到数据库中
    //--6 跳转页面，提示抢课是否成功

    @ApiOperation(value = "添加优惠券", notes = "添加优惠券")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "userId", value = "管理ID", dataType = "Integer", required = true, example = "0"),
            @ApiImplicitParam(name = "teacherId", value = "教师ID", dataType = "Integer", required = true, example = "0"),
            @ApiImplicitParam(name = "price", value = "抢购价格", dataType = "Long", required = true, example = "0"),
            @ApiImplicitParam(name = "classCount", value = "课时数", dataType = "Integer", required = true, example = "0"),
    })
    @PostMapping(value = "/addMarketCoupon")
    public ResponseResult addMarketCoupon(
            @RequestParam("userId") Integer userId,
            @RequestParam("teacherId") Integer teacherId,
            @RequestParam("price") Long price,
            @RequestParam("classCount") Integer classCount
    ) {

        return marketService.addMarketCoupon(userId, teacherId, price, classCount);
    }

    @ApiOperation(value = "抢购优惠券", notes = "抢购优惠券")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "userId", value = "学生ID", dataType = "Integer", required = true, example = "0")
    })
    @PostMapping(value = "/grabMarketClass")
    public ResponseResult grabMarketClass(
            @RequestParam("userId") Integer userId
    ) {

        return marketService.grabMarketClass(userId);
    }

    @ApiOperation(value = "查看优惠券列表", notes = "查看优惠券列表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "userId", value = "学生ID", dataType = "Integer", required = true, example = "0")
    })
    @PostMapping(value = "/getMarketClassByPage")
    public ResponseResult getMarketClassByPage(
            @RequestParam("userId") Integer userId
    ) {

        return marketService.getMarketClassByPage(userId);
    }

    @ApiOperation(value = "开始活动", notes = "开始活动")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "userId", value = "管理员ID", dataType = "Integer", required = true, example = "0")
    })
    @PostMapping(value = "/doBeginGrab")
    public ResponseResult doBeginGrab(
            @RequestParam("userId") Integer userId
    ) {

        return marketService.doBeginGrab(userId);
    }
}
